package com.ruoyi.ldzlsc.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ldzlsc.domain.ScWorkOrderTask;
import com.ruoyi.ldzlsc.domain.vo.*;
import com.ruoyi.ldzlsc.service.IScWorkOrderTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工序任务控制器
 *
 * @author ldzl
 */
@RestController
@RequestMapping("/workordertask")
public class ScWorkOrderTaskController extends BaseController {
    @Autowired
    private IScWorkOrderTaskService workOrderTaskService;

    /**
     * 查询工序任务列表
     */
    @GetMapping("/list")
    public AjaxResult list(ScWorkOrderTask workOrderTask) {
        List<ScWorkOrderTask> list = workOrderTaskService.selectScWorkOrderTaskList(workOrderTask);
        return success(list);
    }

    /**
     * 工序任务开工
     */
    @Log(title = "工序任务开工", businessType = BusinessType.UPDATE)
    @PutMapping("/start/{workOrderTaskId}")
    public AjaxResult start(@PathVariable Long workOrderTaskId) {
        return toAjax(workOrderTaskService.startTask(workOrderTaskId));
    }

    /**
     * 工序任务完成
     */
    @Log(title = "工序任务完成", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{workOrderTaskId}")
    public AjaxResult complete(@PathVariable Long workOrderTaskId) {
        return toAjax(workOrderTaskService.completeTask(workOrderTaskId));
    }

    /**
     * 工序任务暂停
     */
    @Log(title = "工序任务暂停", businessType = BusinessType.UPDATE)
    @PutMapping("/pause/{workOrderTaskId}")
    public AjaxResult pause(@PathVariable Long workOrderTaskId) {
        return toAjax(workOrderTaskService.pauseTask(workOrderTaskId));
    }

    /**
     * 工序任务重启
     */
    @Log(title = "工序任务重启", businessType = BusinessType.UPDATE)
    @PutMapping("/restart/{workOrderTaskId}")
    public AjaxResult restart(@PathVariable Long workOrderTaskId) {
        return toAjax(workOrderTaskService.restartTask(workOrderTaskId));
    }

    /**
     * 获取工序任务详情
     */
    @GetMapping("/{workOrderTaskId}")
    public AjaxResult getDetail(@PathVariable Long workOrderTaskId) {
        WorkOrderTaskDetailVO detail = workOrderTaskService.getWorkOrderTaskDetail(workOrderTaskId);
        return success(detail);
    }

    /**
     * 获取工序任务物料清单
     */
    @GetMapping("/{workOrderTaskId}/materials")
    public AjaxResult getMaterials(@PathVariable Long workOrderTaskId) {
        List<TaskMaterialVO> materials = workOrderTaskService.getTaskMaterials(workOrderTaskId);
        return success(materials);
    }

    /**
     * 获取工序任务报工记录
     */
    @GetMapping("/{workOrderTaskId}/reports")
    public AjaxResult getReports(@PathVariable Long workOrderTaskId) {
        List<TaskReportVO> reports = workOrderTaskService.getTaskReports(workOrderTaskId);
        return success(reports);
    }

    /**
     * 获取工序任务退料记录
     */
    @GetMapping("/{workOrderTaskId}/returns")
    public AjaxResult getReturns(@PathVariable Long workOrderTaskId) {
        List<TaskReturnVO> returns = workOrderTaskService.getTaskReturns(workOrderTaskId);
        return success(returns);
    }

    /**
     * 获取工序任务执行进度
     */
    @GetMapping("/{workOrderTaskId}/progress")
    public AjaxResult getProgress(@PathVariable Long workOrderTaskId) {
        List<TaskProgressVO> progress = workOrderTaskService.getTaskProgress(workOrderTaskId);
        return success(progress);
    }

    /**
     * 获取工序任务派工信息
     */
    @GetMapping("/{workOrderTaskId}/dispatch")
    public AjaxResult getDispatch(@PathVariable Long workOrderTaskId) {
        WorkOrderTaskDetailVO.DispatchInfo dispatch = workOrderTaskService.getTaskDispatch(workOrderTaskId);
        return success(dispatch);
    }
}